package com.javafly.sms.web.cms;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.GrantedAuthority;

import com.javafly.sms.entity.cms.Stest;
import com.javafly.sms.entity.security.Role;
import com.javafly.sms.entity.security.User;
import com.javafly.sms.service.ServiceException;
import com.javafly.sms.service.cms.CmsEntityManager;
import com.javafly.sms.service.security.SecurityEntityManager;
import com.javafly.sms.web.CrudActionSupport;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.hibernate.HibernateWebUtils;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.web.struts2.Struts2Utils;

/**
 * 测试管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数.
 * 演示带分页的管理界面.
 * 
 * @author hym
 */
//定义URL映射对应/cms/stest.action
@Namespace("/cms")
//定义名为reload的result重定向到user.action, 其他result则按照convention默认.
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "stest.action", type = "redirect") })
public class StestAction extends CrudActionSupport<Stest> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private CmsEntityManager cmsEntityManager;

	//-- 页面属性 --//
	private Long id;
	private Stest entity;
	private Page<Stest> page = new Page<Stest>(10);//每页10条记录
	
	//-- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Stest getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = cmsEntityManager.getStest(id);
		} else {
			entity = new Stest();
		}
	}

	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = cmsEntityManager.searchStest(page, filters);
		
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		
		cmsEntityManager.saveStest(entity);
		addActionMessage("保存测试数据成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			cmsEntityManager.deleteStest(id);
			addActionMessage("删除测试数据成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除测试数据失败");
		}
		return RELOAD;
	}

	//-- 其他Action函数 --//
	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		
		Struts2Utils.renderText("true");
		
		//因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	//-- 页面属性访问函数 --//
	/**
	 * list页面显示分页列表.
	 */
	public Page<Stest> getPage() {
		return page;
	}
}
